﻿using UnityEngine;
using System.Collections;

public class ShipInput : MonoBehaviour 
{
	public Vector3 force = new Vector3(0,0,0);
	Vector3 mousePosition;
	public GUIText TextMouse;
	
	public GUIText TextRotation;
	
	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 1F;
    public float sensitivityY = 1F;
 
    public float minimumX = -360F;
    public float maximumX = 360F;
 
    public float minimumY = -60F;
    public float maximumY = 60F;
    float rotationX = 0F;
    float rotationY = 0F;
 
	Quaternion xQuaternion;
 	Quaternion yQuaternion;
	Quaternion originalRotation;
	
	// Use this for initialization
	void Start ()
    {
		Screen.showCursor = false;
		Screen.lockCursor = true;
		Input.mousePosition.Set(0,0,0);
        // Make the rigid body not change rotation
        //if (rigidbody)
            //rigidbody.freezeRotation = true;
        originalRotation = transform.localRotation;
    }
	// Update is called once per frame
	void Update () 
	{
		MouseTarget();
		//guitext.text = mousePosition.normalized.ToString();
		VelocityUpdate();
	}
	
	void MouseTarget()
	{
        if (axes == RotationAxes.MouseXAndY)
        {
            // Read the mouse input axis
            rotationX += Input.GetAxis("Mouse X") * (sensitivityX / 20);
            rotationY += Input.GetAxis("Mouse Y") * (sensitivityY / 20);
 
            rotationX = ClampAngle (rotationX, minimumX, maximumX);
            rotationY = ClampAngle (rotationY, minimumY, maximumY);
 
            xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
            yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
 
            transform.localRotation = originalRotation * xQuaternion * yQuaternion;
        }
		
        else if (axes == RotationAxes.MouseX)
        {
            rotationX += Input.GetAxis("Mouse X") / sensitivityX;
            rotationX = ClampAngle (rotationX, minimumX, maximumX);
 
            xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
            transform.localRotation = originalRotation * xQuaternion;
        }
        else
        {
            rotationY += Input.GetAxis("Mouse Y") / sensitivityY;
            rotationY = ClampAngle (rotationY, minimumY, maximumY);
 
            yQuaternion = Quaternion.AngleAxis (-rotationY, Vector3.right);
            transform.localRotation = originalRotation * yQuaternion;
        }
		
		//force.x = rotationX;
		//force.y = rotationY;
	}
  
 
    public static float ClampAngle (float angle, float min, float max)
    {
        /*if (angle < -360F)
            angle = 0;
        if (angle > 360F)
            angle = 0;*/
        return Mathf.Clamp (angle, min, max);
    }

	void VelocityUpdate ()
	{
		if (Input.GetKeyDown(KeyCode.Space))
			rigidbody.velocity = new Vector3(0, 0, 0);
		
		if (Input.GetKey(KeyCode.A))
		{
			rigidbody.AddRelativeForce(Vector3.forward * 100);
		}
		if (Input.GetKey(KeyCode.S))
		{
			rigidbody.AddRelativeForce( -(Vector3.forward * 100));
		}
	}
}
